<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">
<style>
    #commentForm .main-container xm-select{border: 0 !important;}
    .city-picker-span{border: 0 !important;line-height: 34px !important;height: 34px !important;}
</style>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con">
            <span>新建业绩</span><div class="move_title"></div><?php include "../public/sub.php";?></div>
    </div>
    <div class="main-container">
        <div class="block-first">
            <div class="layui-form-item">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>明细</span></div>
                        <div>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn import_engin">从项目引入</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead class="thead"  style="overflow-y: scroll;">
                        <tr>
                            <th class="width35">序号</th>
                            <th><text>*</text>年份</th>
                            <th><text>*</text>项目名称</th>
                            <th style="width: 120px"><text>*</text>项目类型</th>
                            <th><text>*</text>项目金额(万元)</th>
                            <th style="width: 150px"><text>*</text>项目所在地</th>
                            <th>开工时间</th>
                            <th>竣工时间</th>
                            <th>项目说明</th>
                            <th>项目经理</th>
                            <th>技术负责人</th>
                            <th>附件</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list" style="overflow-y: scroll;min-height: 500px;">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</form>
<script>



    $('.import_engin').click(function () {
        openPage('choose_engin.php','80%','80%');
    });


    function choose_cert(obj, dom){
        var index = $(obj).parents('tr').find('td:first-child').text();
        index = parseInt(index) - 1;
        openPage('../public/choose_cert.php?dom='+dom+'&page=achievemul&index='+index,'80%','80%');
    }


    $('.short_table_add_row').click(function () {
        setTimeout(function () {
            if($('.short_table_tbody tr').length > 0) {
                $('.thead').css('overflow-y','scroll');
            }
        }, 100)
    });


    function short_table_opt_func(bridge_str, table_key, xmObj=[]){
        if(!table_key || table_key == undefined || table_key == 'undefined'){
            table_key = 1;
        }
        update_refresh_row_func();

        $(".short_table_add_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }

            var sle = false;
            var sle_obj;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                }
            });

            if(!sle){
                $(this).parents('.short_table_list').find('table').children('tbody').append(bridge_str);
            }else{
                $(sle_obj).after(bridge_str);
            }

            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 设置滚动条
            if($(this).parents('.short_table_list').find('table').children('tbody').height() >= 500){
                $(this).parents('.short_table_list').find('table').children('thead').css('overflow-y','scroll')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });

        $(".short_table_copy_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj,sle_html;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                    sle_html = '<tr>'+$(this).formhtml()+'</tr>';

                    if($(this).find('.filedetaobjdata').html() != undefined){
                        var filedetaobjdataStr = $(this).find('.filedetaobjdata').html();
                        var filedetaobjdataLength = $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html();
                        if(filedetaobjdataLength > 0){
                            $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html(0);
                        }
                        $(this).find('.filedetaobjdata').html('[]');
                        sle_html = '<tr>'+$(this).formhtml()+'</tr>';
                        if(filedetaobjdataLength > 0){
                            $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html(filedetaobjdataLength);
                        }
                        $(this).find('.filedetaobjdata').html(filedetaobjdataStr);
                    }
                }
            });

            if(!sle){
                layer.msg("请选中要复制的行");return false;
            }else{
                $(sle_obj).after(sle_html);
            }
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 设置滚动条
            if($(this).parents('.short_table_list').find('tbody').height() >= 500){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','scroll')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });

        $(".short_table_del_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                // 样式处理
                $(this).find('td').css({'border': '1px solid #eee'});
                $('.layui-table thead th ').css({'border-bottom-color': '#eee'});

                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                }
            });

            if(!sle){
                layer.msg("请选中要删除的行");return false;
            }else{
                $(sle_obj).remove();
            }

            // if($(this).parents('.short_table_list').find('table').children('tbody').children('tr').length < 1){
            //     $(this).parents('.short_table_list').children('table').children('tbody').append(bridge_str);
            // }

            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 取消滚动条
            if($(this).parents('.short_table_list').find('tbody').height() < 500){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','hidden')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });

        $(".short_table_del_all").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj;
            $(this).parents('.short_table_list').find('table').children('tbody').html('');

            // $(this).parents('.short_table_list').find('table').children('tbody').append(bridge_str);
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                $(this).children('td:first-child').html(key+1);
            });

            // 取消滚动条
            if($(this).parents('.short_table_list').find('tbody').height() < 500){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','hidden')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
            reload_table_list_bac('short_table_list');
        });
    }

    function set_list_render_xmselect(xmArr) {
        if(xmArr){
            for(let i=0;i<xmArr.length;i++){
                let xmObj = xmArr[i];

                if(xmObj && xmObj.name){
                    var types = 'dan';
                    if(xmObj.type && xmObj.type == 'duo'){
                        types = 'duo';
                    }
                    $('.'+xmObj.name).each(function (i) {
                        if(xmObj.name == 'type'){
                            if($(this).attr('class') == xmObj.name){
                                var num = get_rand_xm_classname(xmObj.name);
                                $(this).addClass(xmObj.name+num).attr('key',num);
                                if(types == 'dan'){
                                    xmselect_list_dan(xmObj.name+num, xmObj.data, '');
                                }else{
                                    xmselect_list_duo(xmObj.name+num, xmObj.data, []);
                                }
                            }else{
                                var key = $(this).attr('key');
                                if($("."+xmObj.name+key).length > 1){
                                    var other = $("."+xmObj.name+key)[1];
                                    var xmValue = $(other).find('input').val() || '';
                                    $(other).html('');
                                    var num = get_rand_xm_classname(xmObj.name);
                                    $(other).attr('class', xmObj.name+' '+xmObj.name+num).attr('key',num);
                                    if(types == 'dan'){
                                        xmselect_list_dan(xmObj.name+num, xmObj.data, xmValue);
                                    }else{
                                        xmselect_list_duo(xmObj.name+num, xmObj.data, xmValue.split(","));
                                    }
                                }
                            }
                        }

                        if(xmObj.name == 'addr'){
                            if($(this).attr('class') == xmObj.name){
                                var num = get_rand_xm_classname(xmObj.name);
                                $(this).addClass(xmObj.name+num).attr('key',num).attr('id',xmObj.name+num);
                                load_areas(xmObj.name+num, xmObj.data, 3);
                            }
                        }

                    });
                }
            }
        }
    }


    var addr = [];

    var pub_type;
    var pub_addr;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_achieve_muladd}).done(function(res){
            if (res.code === 2000) {
                addr = res.data.addr;
                pub_type = res.data.type
                pub_addr = res.data.addr



                var str = '';
                str += '<tr>';
                str += '<td class="width35">0</td>';
                str += '<td><input type="text" class="layui-input year bdate_y"></td>';
                str += '<td><input type="text" class="layui-input name"></td>';
                str += '<td style="width: 141px;"><div class="type"></div></td>';
                str += '<td><input type="number" class="layui-input money"></td>';
                str += '<td style="width: 171px;"><input type="text" class="addr"></td>';
                str += '<td><input type="text" class="layui-input start_dt bdate_ymd"></td>';
                str += '<td><input type="text" class="layui-input end_dt bdate_ymd"></td>';
                str += '<td><input type="text" class="layui-input explain"></td>';
                str += '<td><input type="text" readonly name="jingcert" class="layui-input jingcert" onclick="choose_cert(this,\'jingcert\')"></td>';
                str += '<td><input type="text" readonly name="shucert" class="layui-input shucert" onclick="choose_cert(this,\'shucert\')"></td>';
                str += '<td>'+public_show_file_deta('list_list', [],1)+'</td>';
                str += '</tr>';
                short_table_opt_func(str, 1, [{name:'type', data:res.data.type, value:''},{name:'addr', data:res.data.addr, value:''}]);

                render_from();
            }
        });




        form.on('submit(add)', function(data){
            var validRes = $("#commentForm").valid();
            if(!validRes){return false;}

            let list = ['']; var message = '';
            $('.short_table_tbody tr').each(function (i) {
                let year = $(this).find('.year').val();
                let name = $(this).find('.name').val();
                var type = $(this).find('.type').find('input').val();
                let money = $(this).find('.money').val();
                let addr = $(this).find('.addr').val();
                let start_dt = $(this).find('.start_dt').val();
                let end_dt = $(this).find('.end_dt').val();
                let explain = $(this).find('.explain').val();
                let jingcert = $(this).find('.jingcert').attr('key');
                let shucert = $(this).find('.shucert').attr('key');
                let files = public_deta_file_value(this);

                if(!name){
                    message = '第'+(i+1)+'行，请填写项目名称'; return false;
                }
                if(!type){
                    message = '第'+(i+1)+'行，请填写项目类型';return false;
                }
                if(!money){
                    message = '第'+(i+1)+'行，请填写项目金额';return false;
                }
                if(!addr){
                    message = '第'+(i+1)+'行，请填写项目所在地';return false;
                }

                let arr = {
                    year:year,
                    name:name,
                    type:type,
                    money:money,
                    addr:addr,
                    start_dt:start_dt,
                    end_dt:end_dt,
                    explain:explain,
                    jingcert:jingcert,
                    shucert:shucert,
                    files:files
                };
                list.push(arr);
            });



            if(message !== ''){
                facade.error(message); return false;
            }
            if(list.length === 1) {
                facade.error("请填写证书明细"); return false;
            }
            data.field.list = list;

            facade.ajax({url: ask_achieve_muladd, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });
            return false;
        });
    });












    var ctrlv_field_name = [
        {field: 'year', name:'年份',type:'number'},
        {field: 'name', name:'项目名称'},
        {field: 'money', name:'项目金额(万元)'},
        {field: 'start_dt', name:'开工时间',type:'date'},
        {field: 'end_dt', name:'竣工时间',type:'date'},
        {field: 'explain', name:'项目说明'},
    ];
    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#list_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td class="width35">'+nums+'</td>';
                str += '<td><input type="text" class="layui-input year bdate_y" value="'+list[key].year+'"></td>';
                str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                str += '<td style="width: 141px;">';
                str += '<div class="type type'+nums+'" key="'+nums+'"></div>';
                str += '</td>';
                str += '<td><input type="number" class="layui-input money" value="'+list[key].money+'"></td>';
                str += '<td style="width: 171px;"><input type="text" class="addr"></td>';
                str += '<td><input type="text" class="layui-input start_dt bdate_ymd" value="'+list[key].start_dt+'"></td>';
                str += '<td><input type="text" class="layui-input end_dt bdate_ymd" value="'+list[key].end_dt+'"></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '<td><input type="text" readonly name="jingcert" class="layui-input jingcert" onclick="choose_cert(this,\'jingcert\')"></td>';
                str += '<td><input type="text" readonly name="shucert" class="layui-input shucert" onclick="choose_cert(this,\'shucert\')"></td>';
                str += '';
                str += '<td>'+public_show_file_deta('list_list', [],1)+'</td>';

                str += '</tr>';
                $("#list_list").append(str);
                xmselect_list_dan('type'+nums, pub_type, '');
                set_list_render_xmselect([{name:'addr', data:pub_addr, value:''}]);
            }
        }
        render_date();
        render_from();
    }
</script>
</body>
</html>
